#include "view_others_device_info.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "prj_config.h"
#include "times/view_time_0.h"
#include "device_type.h"
const unsigned char num_N310_percentage_19x32[] = {
    /* 数组大小：530 颜色数：17 图片：N411-num-percentage-24x30.bmp*/
    0x18, 0x00, 0x1e, 0x00, 0x11, 0x00, 0x00, 0x00, 0xc7, 0x39, 0x96, 0xb5, 0x9a, 0xd6, 0x14, 0xa5, 0xcf, 0x7b, 0x9e, 0xf7, 0xdf, 0xff, 0x0c, 0x63, 0x04, 0x21, 0xdb, 0xde, 0xd7, 0xbd, 0x10, 0x84,
    0x8a, 0x52, 0x59, 0xce, 0x5d, 0xef, 0x92, 0x94, 0x00, 0x81, 0x01, 0x02, 0x03, 0x81, 0x04, 0x01, 0x00, 0x86, 0x01, 0x04, 0x81, 0x01, 0x00, 0x85, 0x05, 0x06, 0x07, 0x83, 0x06, 0x08, 0x00, 0x85,
    0x02, 0x07, 0x06, 0x09, 0x00, 0x84, 0x09, 0x06, 0x07, 0x81, 0x03, 0x0a, 0x07, 0x81, 0x06, 0x09, 0x00, 0x84, 0x06, 0x07, 0x0b, 0x00, 0x85, 0x0c, 0x07, 0x81, 0x04, 0x00, 0x81, 0x04, 0x07, 0x81,
    0x0c, 0x00, 0x83, 0x05, 0x07, 0x81, 0x05, 0x00, 0x85, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x83, 0x0b, 0x07, 0x06, 0x00, 0x86, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81,
    0x08, 0x07, 0x81, 0x04, 0x00, 0x82, 0x09, 0x06, 0x07, 0x0b, 0x00, 0x86, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x82, 0x0c, 0x07, 0x81, 0x0d, 0x00, 0x86, 0x04, 0x07,
    0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x82, 0x0e, 0x07, 0x0f, 0x00, 0x87, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x81, 0x09, 0x07, 0x81, 0x02, 0x00,
    0x87, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x81, 0x0c, 0x07, 0x81, 0x01, 0x00, 0x87, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x81, 0x0a,
    0x07, 0x0a, 0x00, 0x88, 0x10, 0x07, 0x81, 0x0c, 0x00, 0x81, 0x0c, 0x07, 0x81, 0x10, 0x00, 0x01, 0x07, 0x81, 0x10, 0x00, 0x88, 0x01, 0x07, 0x81, 0x06, 0x10, 0x81, 0x06, 0x07, 0x81, 0x01, 0x00,
    0x10, 0x07, 0x81, 0x01, 0x00, 0x89, 0x02, 0x07, 0x85, 0x02, 0x00, 0x81, 0x0f, 0x07, 0x03, 0x00, 0x8b, 0x10, 0x0f, 0x07, 0x81, 0x0f, 0x0c, 0x00, 0x81, 0x0d, 0x07, 0x81, 0x0c, 0x00, 0x81, 0x01,
    0x10, 0x03, 0x81, 0x10, 0x01, 0x00, 0x8b, 0x02, 0x07, 0x81, 0x09, 0x00, 0x0d, 0x06, 0x07, 0x83, 0x06, 0x0d, 0x00, 0x8a, 0x0f, 0x07, 0x0e, 0x00, 0x09, 0x06, 0x07, 0x81, 0x0a, 0x81, 0x07, 0x81,
    0x06, 0x09, 0x00, 0x88, 0x08, 0x07, 0x81, 0x0c, 0x00, 0x0c, 0x07, 0x81, 0x0b, 0x00, 0x81, 0x0b, 0x07, 0x81, 0x05, 0x00, 0x88, 0x0b, 0x07, 0x06, 0x00, 0x81, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81,
    0x08, 0x07, 0x81, 0x04, 0x00, 0x88, 0x06, 0x07, 0x0b, 0x00, 0x81, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x87, 0x05, 0x07, 0x81, 0x05, 0x00, 0x81, 0x04, 0x07, 0x81,
    0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x87, 0x0b, 0x07, 0x0f, 0x00, 0x82, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x86, 0x09, 0x07, 0x81, 0x02, 0x00, 0x82,
    0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x86, 0x0c, 0x07, 0x81, 0x0d, 0x00, 0x82, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x86, 0x0e, 0x07,
    0x0f, 0x00, 0x83, 0x04, 0x07, 0x81, 0x08, 0x00, 0x81, 0x08, 0x07, 0x81, 0x04, 0x00, 0x85, 0x01, 0x07, 0x81, 0x04, 0x00, 0x83, 0x04, 0x07, 0x81, 0x05, 0x00, 0x81, 0x05, 0x07, 0x81, 0x04, 0x00,
    0x85, 0x10, 0x07, 0x81, 0x01, 0x00, 0x83, 0x0d, 0x07, 0x81, 0x0a, 0x05, 0x81, 0x0a, 0x07, 0x81, 0x0d, 0x00, 0x85, 0x0a, 0x07, 0x0a, 0x00, 0x85, 0x0b, 0x07, 0x85, 0x0b, 0x00, 0x85, 0x01, 0x07,
    0x81, 0x10, 0x00, 0x85, 0x09, 0x02, 0x07, 0x83, 0x02, 0x09, 0x00, 0x91, 0x09, 0x08, 0x81, 0x09, 0x00, 0x82,
};

void view_others_device_info_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[4] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[6] %= 12;
            if (disp_ctrl.com_data.i_temp[6] == 0)
            {
                disp_ctrl.com_data.i_temp[6] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[7] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[6], disp_ctrl.com_data.i_temp[7]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 65, 36, FILE_IMG_E205_electricity_0_65_36);
    int x = disp_draw_user_image_num_mid_ignore(120, 104, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[4], 14 + 2, img_j401_TAB);
    disp_draw_user_image(x, 104, img_j401_percent);

    uint8_t battery_level = battery_ctrl_get_level(disp_ctrl.com_data.i_temp[4]);
    if (disp_ctrl.com_data.i_temp[8])
    {
        disp_ctrl.auto_refresh_time = 500;
        // if (battery_level == 0)
        // {
        //     battery_level = disp_ctrl.auto_refresh_count % 2;
        // }
        // else if (battery_level < 4) //充电格
        // {
        //     battery_level = disp_ctrl.auto_refresh_count % (battery_level + 1) + 1;
        // }
        battery_level = disp_ctrl.auto_refresh_count % 5;
        for (uint8_t i = 0; i < battery_level; i++)
        {
            disp_draw_rect_color(68 + i * 26, 39, 68 + i * 26 + 25 - 1, 39 + 51 - 1, RGB565_COLOR_WHITE);
        }
    }
    else
    {
        if (battery_level == 1)
        {
            disp_draw_rect_color(68, 39, 68 + 25 - 1, 39 + 51 - 1, 0xf986);
        }
        else
        {
            for (int i = 0; i < battery_level; i++)
            {
                disp_draw_rect_color(68 + i * 26, 39, 68 + i * 26 + 25 - 1, 39 + 51 - 1, RGB565_COLOR_WHITE);
            }
        }
    }

    // ble_gap_addr_t p_addr;
    // sd_ble_gap_address_get(&p_addr);
    // uint8_t mac_addr[4];

    // mac_addr[0] = ((p_addr.addr[1] >> 4) & 0x0F);
    // mac_addr[1] = (p_addr.addr[1] & 0x0F);
    // mac_addr[2] = ((p_addr.addr[0] >> 4) & 0x0F);
    // mac_addr[3] = (p_addr.addr[0] & 0x0F);
    // char DeviceName[20] = {0};
    // sprintf(DeviceName, "%s%01X%01X%01X%01X", DEVICE_ADV_NAME, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3]);

    // disp_show_user_string_middle(0, 240, 176, DeviceName, num_hex_12x28_get_bmp);
    char buffer[20] = {0};
    uint16_t len = 16;
    memset(buffer, 0, sizeof(buffer));
    sd_ble_gap_device_name_get((uint8_t *)buffer, &len);
    disp_show_user_string_middle(0, 240, 176, buffer, num_hex_12x28_get_bmp);
}

void view_others_device_info_detail_display(void)
{
    // uint8_t y_offset = 0;
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.auto_refresh_time = 500;
        memset(&disp_ctrl.com_data.uint8_temp[100], 0, 64);
        get_show_device_name(&disp_ctrl.com_data.uint8_temp[100], 32);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);

    uint8_t hardware[32] = {0};
    uint8_t firmware[32] = {0};
    strcpy((char *)hardware, (char *)&disp_ctrl.com_data.uint8_temp[100]);
    sprintf((char *)hardware, "%s_V%s", hardware, HARDWARE_VERSION);
    sprintf((char *)firmware, "%s_%s", FIRMWARE_VERSION, FW_STRING_DATE);

//硬件版本号
#define DEVICE_MAC_HW_START_Y (42)
    disp_show_user_string_middle(0, 240, DEVICE_MAC_HW_START_Y, (char *)hardware, num_hex_12x28_get_bmp);

//固件版本号
#define DEVICE_MAC_FW_START_Y (125 - 30)
    disp_show_user_string_middle(0, 240, DEVICE_MAC_FW_START_Y, (char *)firmware, num_hex_12x28_get_bmp);

//蓝牙地址
#define DEVICE_MAC_MAC_START_Y (215 - 60)
    char buffer[32] = {0};
    memset(buffer, 0, sizeof(buffer));
    ble_gap_addr_t p_addr;
    sd_ble_gap_address_get(&p_addr);
    sprintf(buffer, "%1X%1X:%1X%1X:%1X%1X:%1X%1X:%1X%1X:%1X%1X", p_addr.addr[5] >> 4, p_addr.addr[5] & 0xf, p_addr.addr[4] >> 4, p_addr.addr[4] & 0xf, p_addr.addr[3] >> 4, p_addr.addr[3] & 0xf,
            p_addr.addr[2] >> 4, p_addr.addr[2] & 0xf, p_addr.addr[1] >> 4, p_addr.addr[1] & 0xf, p_addr.addr[0] >> 4, p_addr.addr[0] & 0xf);

    disp_show_user_string_middle(0, 240, DEVICE_MAC_MAC_START_Y, buffer, num_hex_9x19_get_bmp); //总长度7+16+14=37*6-7=215
}
